 clear all;addpath('auxiliar'); addpath('Simulated_Datasets')
load SIMULATION_ERGODIC_DE
YSIM=priceSIM.*yNSIM+ySIM;
mY=mean(YSIM);

% DEFAULT SETS FOR DIFFERENT LEVELS OF DEBT (HIGH OR LOW
Debt_high=20;
Debt_low=10;
mKappa=1; % THE LOWEST KAPPA
mPV=3; % THE THIRD LEVEL OF SPREAD

maxy_defL=zeros(NBG,1);
maxy_defH=zeros(NBG,1);
maxy_defL=zeros(NBG,2);
set0=squeeze(Def_func(:,Debt_low,((mKappa*mPV)*NT+1):((mKappa*mPV)*NT+NT)));
set=set0;
set(set0>=.5)=1;
set(set0<.5)=0;

 for i=1:NBG
     a=find(set(i,:) >= 1, 1, 'last');
     if isempty(a)
       maxy_defL(i)=yT_values(1)-.01;
     else
         maxy_defL(i)=yT_values(a);
     end
 end

    set0=squeeze(Def_func(:,Debt_high,((mKappa*mPV)*NT+1):((mKappa*mPV)*NT+NT)));
set=set0;
set(set0>=.5)=1;
set(set0<.5)=0;

 for i=1:NBG
     a=find(set(i,:) >= 1, 1, 'last');
     if isempty(a)
       maxy_defH(i)=yT_values(1)-.01;
     else
         maxy_defH(i)=yT_values(a);
     end
 end

  for i=1:NBG
    max_def(i,1)= maxy_defL(i);
    max_def(i,2)= maxy_defH(i)-maxy_defL(i);
  end


    figure 
  area(BGgrid/mY,max_def)
    ylim([yT_values(1)-.005,yT_values(NT)])
    xlim([BGgrid(1),BGgrid(NBG)]*cte_debtfactor/mY)
    xlabel("Current public debt" ,'Interpreter','Latex')
    ylabel("Income shock",'Interpreter','Latex')
    %title(title_var)
    legend({'Default set with low current private debt','Default set with high current private debt'},'Location','northwest','Box','off')
    hold off
  exportfig(gcf,'Plots\Fig5A_DefaultZones_Income.eps','color','cmyk','resolution',1600)
bgrid1d(Debt_low)/mY
bgrid1d(Debt_high)/mY

clear all;addpath('auxiliar'); addpath('Simulated_Datasets')
load SIMULATION_ERGODIC_DE
YSIM=priceSIM.*yNSIM+ySIM;
mY=mean(YSIM);

%mKappa=fix(NKAPPA/2)+1;
%mPV=fix(NPV/2)+1;
%st=(mKappa*mPV)*NT+1;
st=25;
%st=1;
def_func_extrac=squeeze(Def_func(:,:,st));



maxy_defL=zeros(NB,1);
maxy_defH=zeros(NB,1);
maxy_defL=zeros(NB,2);
set0=def_func_extrac; %squeeze(Def_func(:,Debt_low,((mKappa*mPV)*NT+1):((mKappa*mPV)*NT+NT)));
set=set0;
set(set0>=.5)=1;
set(set0<.5)=0;

 for i=1:NB
     a=find(set(:,i) >= 1, 1, 'first');
     a1(a)=a;
     if isempty(a)
       maxy_defL(i)=BGgrid(1)-.05;
     else
         maxy_defL(i)=BGgrid(a);
     end
 end

    

  for i=1:NB
    max_def(i,1)= maxy_defL(i);
   % max_def(i,2)= maxy_defH(i)-maxy_defL(i);
  end

figure
  area(bgrid1d/mY,BGgrid(NBG)*cte_debtfactor/mY*ones(NB,1),'FaceColor',[0.8500 0.3250 0.0980]) ;hold on 
  area(bgrid1d/mY,max_def*cte_debtfactor/mY,'FaceColor',[0 0.4470 0.7410]); hold on
      txt = 'Default Zone'; hold on
      text(0.1,.3,txt,'FontSize',14); hold on

  txt = 'Repayment Zone '; hold on
      text(0.1,.1,txt,'FontSize',14); hold on

    xlim([bgrid1d(1)/mY,bgrid1d(NB)/mY])
    ylim([BGgrid(1)*cte_debtfactor/mY,BGgrid(NBG)*cte_debtfactor/mY])
  

    ylabel("Current public debt",'Interpreter','Latex');
    xlabel("Current private debt",'Interpreter','Latex');

   %title(title_var)

    %legend({'Repayment set at mean shock'},'Location','northwest','Box','off')
    hold off
    fig = gcf;
    exportfig(gcf,'Plots\Fig5B_DefaultZones.eps','color','cmyk','resolution',1600)